package service;

import java.io.File;
import java.util.List;

import dao.MyFileDao;

import entity.MyFile;

public class FileService {
	
	MyFileDao mfd = new MyFileDao();
	
	public List<MyFile> findByFolderId( int folderId){
		return mfd.findByFolderId(folderId);
	}

	public void delById(int id) {
	    MyFile myFile = findByFileId(id);
	    List<MyFile> myFiles = findByMD5(myFile.getName().substring(0, myFile.getName().lastIndexOf(".")));
	    System.out.println(myFiles.size());
	    if (myFiles.size() == 1) {
    	    File file = new File("D:/upload", myFile.getName());
    	    file.delete();
	    }
		mfd.delFile(id);
		
	}
	public void delByFolderID(int id){
		mfd.delBYFolderID(id);
	}
	
	public void addFile(MyFile file) {
		mfd.addFile(file);
	}
	
	public MyFile findByFileId(int id) {
		return mfd.findByFileId(id);
	}
	
	public List<MyFile> findByMD5(String md5) {
	    return mfd.findByMD5(md5);
	}
}
